package daoimpl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import vo.Employee;
import vo.Payment;
import vo.Saleorder;
import vo.Saleorderitem;

import com.opensymphony.xwork2.ActionContext;

import dao.BaseDAO;
import dao.ISaleDao;

public class SaleDao extends BaseDAO implements ISaleDao{
	//1.根据姓名和密码获得验证
	public Employee getEmployee(String name,String password){
		
		Session session=getSession();
		
		String hql="from Employee  where name='"+name+"' and password='"+password+"'";
		Query query=session.createQuery(hql);
		List employees=query.list();
		System.out.println("testDao6");
		if(employees.size()!=0){
			Employee employee=(Employee)employees.get(0);
			return employee;
		}
		session.close();
		
		
        return null;
	}

	
	//=======================================2.添加订单===============================================
	public void addSaleorder(Saleorder saleorder) {
		Session session=getSession();
		Transaction tx=session.beginTransaction();
		session.save(saleorder);
		tx.commit();
		session.close();
		
	}
	//=======================================2.更新订单===============================================
			public void updateSaleorder(Saleorder saleorder) {
				Session session=getSession();
				Transaction tx=session.beginTransaction();
				session.update(saleorder);
				tx.commit();
				session.close();
				
			}


	//=======================================2.添加订单明细表===============================================
	/**public void addShowitem(Showitem item) {
		Session session=getSession();
		Transaction tx=session.beginTransaction();
		session.save(item);
		tx.commit();
		session.close();
		
	};**/

	public void addSaleorderitem(Saleorderitem saleorderitem) {
		Session session=getSession();
		Transaction tx=session.beginTransaction();
		session.save(saleorderitem);
		tx.commit();
		session.close();
		
	}

	//===============3.获得明细表所有记录====================
		public List<Saleorderitem> getAllItem(){
			 Map sessiona=ActionContext.getContext().getSession();
		   	   Saleorder saleorder2=(Saleorder)sessiona.get("saleorder");
		   	   
		   	 
		 if(saleorder2!=null) {
			  Session session=getSession();
	    	  Query query=session.createQuery("from Saleorderitem s where saleorderid="+saleorder2.getSaleorderid());
	    	  List saleorderitem=query.list();
	  		
	  		session.close();
	  		return saleorderitem;}
		   	  else
		   		  return null;
		}

		
		//===============6.获得临时表的总金额===================
		public float getTotalPrice() {
			List<Saleorderitem> showitems= getAllItem();
			float total=0;
			for(int i=0;i<showitems.size();i++){
				Saleorderitem showitem=showitems.get(i);
				total+=showitem.getPrice()*showitem.getQuantity();
			}
			return total;
		}
		//===============7.添加结算记录===================
		public void addPayment(Payment payment) {
			Session session=getSession();
			Transaction tx=session.beginTransaction();
			session.save(payment);
			tx.commit();
			session.close();
			
		}
		//================8.通过saleorderid获得所有订单明细===
		public List<Saleorderitem> getAllItemById(String saleorderid){
			
			  Session session=getSession();
	    	  Query query=session.createQuery("from Saleorderitem s where saleorderid="+saleorderid);
	    	  List saleorderitem=query.list();
	  		
	  		session.close();
	  		return saleorderitem;
		   	 
		}
		//================9.通过hql语句操作数据库表saleorderitem==
	  public void sale_updateByHql(String hql){
		  Session session=getSession();
		  Query query=session.createQuery(hql);
		  query.executeUpdate();
		  session.close();
		  
	  }
	//================10.通过saleorderid获得订单明细表记录==
	  public Saleorderitem getSaleorderitemById(String saleorderitemid){
		  Session session=getSession();
    	  Query query=session.createQuery("from Saleorderitem s where saleorderitemid="+saleorderitemid);
    	  List list=query.list();
    	  Saleorderitem  saleorderitem=(Saleorderitem) list.get(0);
  		session.close();
  		return saleorderitem;
	  }
		//================9.通过hql语句操作数据库表saleorderitem==
	  public void sale_updateSaleorderByHql(String hql){
		  Session session=getSession();
		  Query query=session.createQuery(hql);
		  query.executeUpdate();
		  session.close();
		  
	  }
	//================12.通过saleorderid获得订单表记录=======
	  public Saleorder getSaleorderByHql(String hql){
		  Session session =getSession();
		  Query query=session.createQuery(hql);
		  List list=query.list();
		  Saleorder saleorder=(Saleorder)list.get(0);		
		  return saleorder;
	  }
	  
}
